package com.saturn.titan.core.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.saturn.titan.core.bean.BusiDeployBill;
import com.saturn.titan.core.bean.BusiInstallBill;
import com.saturn.titan.core.bean.BusiRepairBill;
import com.saturn.titan.core.bean.BusiWorkBill;
import com.saturn.titan.core.comm.BillType;
import com.saturn.titan.core.comm.Constants;
import com.saturn.titan.core.repository.mybatis.BusiDeployBillDao;
import com.saturn.titan.core.repository.mybatis.BusiIntsallBillDao;
import com.saturn.titan.core.repository.mybatis.BusiRepairBillDao;
import com.saturn.titan.core.repository.mybatis.BusiWorkBillDao;
import com.saturn.titan.web.utils.CRMUtil;

@Component
@Transactional(readOnly = true)
public class DeployBillService {

	private static Logger logger = LoggerFactory.getLogger(ReportService.class);
	
	@Autowired
	private BusiDeployBillDao busiDeployBillDao;
	
	@Autowired
	private BusiWorkBillDao busiWorkBillDao;
	
	@Autowired
	private BusiIntsallBillDao busiIntsallBillDao;
	
	@Autowired
	private BusiRepairBillDao busiRepairBillDao;
	
	@Autowired
	private BillNoService billNoService;

	@Transactional(readOnly = false)
	public void insert(BusiDeployBill bill){
		busiDeployBillDao.insert(bill);
	}
	
	public BusiDeployBill findById(Long id){
		return busiDeployBillDao.selectById(id);
	}
	
	/**
	 * 根据工单id，获取当前分配人员id
	 * @return
	 */
	public Long findCurrentEmpByWorkBillId(Long id){
		return busiDeployBillDao.findCurrentEmpByWorkBillId(id);
	}

	/**
	 * 根据被分派员工，进行分派
	 * @param pointId 员工或经销商id
	 * @param workBillId
	 */
	@Transactional(readOnly = false)
	public void deployEmpWorkBill(Long empId, Long workBillId) {
		
		//生成派工单
		BusiWorkBill wb = busiWorkBillDao.selectById(workBillId);
		BusiDeployBill db = new BusiDeployBill();
		db.setEmpId(empId);
		db.setWorkBillId(workBillId);
		db.setWorkBillNo(db.getBillNo());
		db.setOrgId(wb.getOrgId());
		db.setBillNo(billNoService.getNextBillNo(BillType.DB));
		busiDeployBillDao.insert(db);
		
		//生成维修单或安装单
		createInstallOrRepareBill(empId,wb,db,Constants.DEPLOY_TYPE_EMP);
		
		//修改工单状态
		wb.setBillStt(1);
		busiWorkBillDao.updateColumns( wb,"billStt");
	}
	/**
	 * 根据被分派经销商ID，进行分派
	 * @param pointId 员工或经销商id
	 * @param workBillId
	 */
	public void deployDealerWorkBill(Long dealerId, Long workBillId) {
		
		//生成派工单
		BusiWorkBill wb = busiWorkBillDao.selectById(workBillId);
		BusiDeployBill db = new BusiDeployBill();
		db.setDealerId(dealerId);
		db.setWorkBillId(workBillId);
		db.setWorkBillNo(db.getBillNo());
		db.setOrgId(wb.getOrgId());
		db.setBillNo(billNoService.getNextBillNo(BillType.DB));
		CRMUtil.setBusiBillOprFileForInsert(db);
		
		busiDeployBillDao.insert(db);
		
		createInstallOrRepareBill(dealerId	,wb,db,Constants.DEPLOY_TYPE_DEALER);
		
	}

	private void createInstallOrRepareBill(Long operatorId, BusiWorkBill wb,BusiDeployBill db,
			Integer deployTypeDealer) {
		if(Constants.REQUEST_TYPE_INSTALL.equals(wb.getRequestType())){
			BusiInstallBill ib = new BusiInstallBill();
			ib.setWorkBillId(wb.getId());
			ib.setOrgId(wb.getOrgId());
			ib.setDeployBillId(db.getId());
			ib.setBillNo(billNoService.getNextBillNo(BillType.IB));
			ib.setEmergentType(wb.getEmergentType());
			if(Constants.DEPLOY_TYPE_DEALER == deployTypeDealer){
				ib.setDealerId(operatorId);
			}else{
				ib.setEmpId(operatorId);
			}
			ib.setBillStt(Constants.OP_BILL_STT_ACTIVE);
			CRMUtil.setBusiBillOprFileForInsert(ib);
			db.setBillNo(billNoService.getNextBillNo(BillType.IB));
			busiIntsallBillDao.insert(ib);
		}else{
			BusiRepairBill repairBill = new BusiRepairBill();
			repairBill.setWorkBillId(wb.getId());
			repairBill.setDeployBillId(db.getId());
			repairBill.setOrgId(wb.getOrgId());
			repairBill.setBillNo(billNoService.getNextBillNo(BillType.RB));
			repairBill.setEmergentType(wb.getEmergentType());
			if(Constants.DEPLOY_TYPE_DEALER == deployTypeDealer){
				repairBill.setDealerId(operatorId);
			}else{
				repairBill.setEmpId(operatorId);
			}
			repairBill.setBillStt(Constants.OP_BILL_STT_ACTIVE);
			CRMUtil.setBusiBillOprFileForInsert(repairBill);
			db.setBillNo(billNoService.getNextBillNo(BillType.RB));
			busiRepairBillDao.insert(repairBill);
		}
		
	}
	
	/**
	 * 坐席分派给区域主管
	 */
	@Transactional(readOnly = false)
	public void AreaDeployAssign(Long empId, Long workBillId){
		//生成派工单
			BusiWorkBill wb = busiWorkBillDao.selectById(workBillId);
			BusiDeployBill db = new BusiDeployBill();
			db.setEmpId(empId);
			db.setWorkBillId(workBillId);
			db.setWorkBillNo(db.getBillNo());
			db.setOrgId(wb.getOrgId());
			db.setBillNo(billNoService.getNextBillNo(BillType.DB));
			db.setBillStt(0);
			busiDeployBillDao.insert(db);
			
			List<BusiDeployBill> list = busiDeployBillDao.select(db);
			
			if(null!=list){
				Long deployId = list.get(0).getId();
				wb.setAssignBillId(deployId);
			}
			//修改工单状态
			wb.setBillStt(6);
			busiWorkBillDao.updateColumns( wb,"billStt");
			busiWorkBillDao.updateColumns( wb,"assignBillId");
	}
	
	@Transactional(readOnly = false)
	public void updateAssignEmp(BusiDeployBill bdb){
		busiDeployBillDao.updateColumns( bdb,"empId");
	}
	
	
	public Long findCurrentDeployIdByWorkBillId(Long id){
		return busiDeployBillDao.findCurrentDeployIdByWorkBillId(id);
	}
}
